<!DOCTYPE HTML>
<html>
<!--
  https://bugzilla.mozilla.org/show_bug.cgi?id=497003

  This test verifies that partially cached content is read from the cache first
  and then from the network. It is written in the mochitest framework to take
  thread retargeting into consideration of nsIStreamListener callbacks (inc.
  nsIRequestObserver). E.g. HTML5 Stream Parser requesting retargeting of
  nsIStreamListener callbacks to the parser thread.
-->
<head>
  <meta charset="UTF-8">
  <title>Test for Bug 497003: support sending OnDataAvailable() to other threads</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <p><a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=497003">Mozilla Bug 497003: support sending OnDataAvailable() to other threads</a></p>
  <p><iframe id="contentFrame" src="partial_content.sjs"></iframe></p>

<pre id="test">
<script>



/* Check that the iframe has initial content only after the first load.
 */
function expectInitialContent(e) {
  info("expectInitialContent",
       "First response received: should have partial content");
  var frameElement = document.getElementById('contentFrame');
  var frameWindow = frameElement.contentWindow;

  // Expect "First response" in received HTML.
  var firstResponse = frameWindow.document.getElementById('firstResponse');
  ok(firstResponse, "First response should exist");
  if (firstResponse) {
    is(firstResponse.innerHTML, "First response",
       "First response should be correct");
  }

  // Expect NOT to get any second response element.
  var secondResponse = frameWindow.document.getElementById('secondResponse');
  ok(!secondResponse, "Should not get text for second response in first.");

  // Set up listener for second load.
  removeEventListener("load", expectInitialContent, false);
  frameElement.addEventListener("load", expectFullContent, false);

  // Reload.
  frameElement.src="partial_content.sjs";
}

/* Check that the iframe has all the content after the second load.
 */
function expectFullContent(e)
{
  info("expectFullContent",
       "Second response received: should complete content from first load");
  var frameWindow = document.getElementById('contentFrame').contentWindow;

  // Expect "First response" to still be there
  var firstResponse = frameWindow.document.getElementById('firstResponse');
  ok(firstResponse, "First response should exist");
  if (firstResponse) {
    is(firstResponse.innerHTML, "First response",
       "First response should be correct");
  }

  // Expect "Second response" to be there also.
  var secondResponse = frameWindow.document.getElementById('secondResponse');
  ok(secondResponse, "Second response should exist");
  if (secondResponse) {
    is(secondResponse.innerHTML, "Second response",
       "Second response should be correct");
  }

  SimpleTest.finish();
}

// Set listener for first load to expect partial content.
// Note: Set listener on the global object/window since 'load' should not fire
// for partially loaded content in an iframe.
addEventListener("load", expectInitialContent, false);

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>
